package motif

import (
	"math"
)

type Column []float64

type PWM []Column

// maps 2-bit ints to nucleotide letters
var BitsToNucleotide = [4]byte{
	0: 'A',
	1: 'C',
	2: 'G',
	3: 'T',
}

// maps nucleotide letters to 2-bit ints
var NucleotideToBits [256]uint8

// fill the above on package initialization
func init() {
	for i, v := range BitsToNucleotide {
		NucleotideToBits[v] = uint8(i)
	}
}

func (p PWM) ICPC() float64 {
	sum := 0.
	for _, c := range p {
		sum += c.ICPC()
	}

	return sum/float64(len(p))
}

func (c Column) ICPC() float64 {
	n := float64(len(c))
	logsum := 0.
	for _, f := range c {
		if f != 0 {
			// this will crash if f < 0, but it never should be; if it is,
			// something is wrong _outside_ of this function, and I want to
			// know about it asap, so let it crash
			logsum += f*math.Log2(n*f)
		}
	}

	return logsum
}

